import { Color, Mesh, Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param attributeName string
 * @param newValue integer representing a hex color value
 * @constructor
 */
class SetColorCommand<T extends Object3D,K extends keyof T> extends Command {
	private object:T;
	private attributeName:K;
	private oldValue:number|undefined;
	private newValue:number;
	constructor( editor:Editor, object:T, attributeName:K, newValue:number ) {

		super( editor );

		this.type = 'SetColorCommand';
		this.name = `Set ${attributeName.toString()}`;
		this.updatable = true;

		this.object = object;
		this.attributeName = attributeName;
		this.oldValue = ( object !== undefined ) ? (this.object[ this.attributeName ] as Color).getHex() : undefined;
		this.newValue = newValue;

	}

	execute() {

		(this.object[ this.attributeName ] as Color).setHex( this.newValue );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	undo() {
		if(this.oldValue!=undefined)
		(this.object[ this.attributeName ] as Color).setHex( this.oldValue );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	update( cmd:SetColorCommand<T,K> ) {

		this.newValue = cmd.newValue;

	}

	

}

export { SetColorCommand };
